package vip.xiaonuo.dev.modular.job.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 历史话单接口工具类
 */
@Component
public class HttpApiUtil {

    private static final Logger log = LoggerFactory.getLogger(HttpApiUtil.class);

    private static final String preUrl = "http://183.247.194.62:53333";
    // http://183.247.194.62:5005/data/e_cdr_20240826/1724651004000/1724651000000

    public static String getDapanContent(){
        String url = "http://m.12321.cn";
        String result = "";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url,HttpMethod.GET, httpEntity, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.hasBody()) {
            String responseBody = responseEntity.getBody();
            Document doc = Jsoup.parse(responseBody);
            Elements recordElements = doc.select("p.record");
            result = recordElements.stream()
                    .flatMap(record -> record.select("span").stream())  // 提取每个p.record中的span标签
                    .map(Element::text)  // 获取span标签的文本内容
                    .collect(Collectors.joining("\n"));  // 用换行符连接所有span内容
            // 提取出数字
            System.out.println(result);
        }
        return result;
    }
    public static void main(String[] args) {
        HttpApiUtil util = new HttpApiUtil();
        String url = "http://183.247.194.62:63636/login";
        String username = "a15558558858";
        String password = "Aa123456";
        url = "http://183.247.194.62:63636/records";
        url = "http://183.247.194.62:63636/records/TmorMVBUNFZPVGNySzRHUFk4azBVUT09";
        String cookies = "6nm4ev6efp1g5d28biupl2aoi5";
        util.getTousuDetail(url, cookies);
    }

    public static String getTousuLogin(String url, String username, String password) {
        String result = "";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        String requestBody = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}";
        httpHeaders.set("Content-Type", "application/json");
        HttpEntity<String> httpEntity = new HttpEntity<String>(requestBody, httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url,HttpMethod.POST, httpEntity, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.hasBody()) {
            String responseBody = responseEntity.getBody();
            System.out.println(responseBody);
            try {
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                // 获取cookies
                JSONObject cookies = jsonObject.getJSONObject("cookies");
                // 获取advanced-api
                String advancedAPI = cookies.getString("advanced-api");
                System.out.println(advancedAPI);
            } catch (Exception e) {
                log.error("解析JSON出错", e);
            }
        }
        return result;
    }

    public static JSONArray getTousuList(String url, String cookies) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Cookie", cookies);
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url,HttpMethod.GET, httpEntity, String.class);
        JSONArray result = new JSONArray();
        if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.hasBody()) {
            String responseBody = responseEntity.getBody();
            System.out.println(responseBody);
            try {
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                JSONObject data = jsonObject.getJSONObject("data");
                result = data.getJSONArray("data");
                System.out.println(result.toJSONString());
                return result;
            } catch (Exception e) {
                log.error("解析JSON出错", e);
            }
        }
        return result;
    }

    public static JSONObject getTousuDetail(String url, String cookies) {
        System.out.println(url);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Cookie", cookies);
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url,HttpMethod.GET, httpEntity, String.class);
        JSONObject result = new JSONObject();
        if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.hasBody()) {
            String responseBody = responseEntity.getBody();
            System.out.println(responseBody);
            try {
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                JSONObject data = jsonObject.getJSONObject("data");
                result = data;
                System.out.println(result.toJSONString());
                return result;
            } catch (Exception e) {
                log.error("解析JSON出错", e);
            }
        }
        return result;
    }
}
